package
{
	import flash.display.Sprite;
	import flash.events.NetStatusEvent;
	import flash.events.SyncEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.SharedObject;

	public class TestNetGroup extends Sprite
	{
		private var video:Video;

		private var nc:NetConnection;

		private var ns:NetStream;

		private const SERVER:String = "rtmfp://192.168.1.209/HelloWorld/";

		private var streamData:SharedObject;

		private var entries_so:SharedObject;

		private var isConnected:Boolean = false;

		public function TestNetGroup()
		{
			nc = new NetConnection();
			//			nc.connect("rtmp://localhost/LiveStreams");
			
			nc.client = {clientFun: function(info:Object):void
			{
				trace("接受到服务器端数据？");
				trace(JSON.stringify(info));
				
				nc.call("serverFun",null,"测试客户端发送数据到服务器！");
				
			}};
			
			nc.connect(SERVER);
			nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler);


		}

		private function onNetStatusHandler(evt:NetStatusEvent):void
		{
			trace(evt.info.code);
			switch (evt.info.code)
			{
				case "NetConnection.Connect.Success":
				{

					isConnected = true;

					//Set up shared object
//					streamData = SharedObject.getRemote("streamData", nc.uri, false);
//					streamData.connect(nc);
//					streamData.addEventListener(SyncEvent.SYNC, seeSO);

					entries_so = SharedObject.getRemote("entries_so", nc.uri, false);
					entries_so.connect(nc);
					entries_so.addEventListener(SyncEvent.SYNC, seeSO);
					break;
				}
				case "NetStream.Connect.Success": // e.info.stream
					video = new Video();
					video.attachNetStream(ns);
					ns.play("stream");
					addChild(video);
					break;
				default:
				{
					break;
				}
			}
		}

		private function seeSO(se:SyncEvent):void
		{
			trace(se.changeList[0].code);
			switch (se.changeList[0].code)
			{
				case "clear":
					trace(JSON.stringify(entries_so.data));

					if (entries_so.data.hasOwnProperty("groupspecWithAuthorizations"))
					{
						ns = new NetStream(nc, entries_so.data.groupspecWithAuthorizations);
						ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler);
					}
					break;
				case "success":
					trace(JSON.stringify(entries_so.data));
					break;
				case "change":
					trace(JSON.stringify(entries_so.data));
					break;
			}
		}
	}
}
